#!/usr/bin/perl -wc

package RFCMail::ESMTP::Server::Query::SenderMX;

use strict;
use RFCMail::DNS::MXData;
use RFCMail::ESMTP::Object::ReversePath;


sub mail_from {
    my $self = shift;
    my ($from) = @_;
    delete($self->{state}->{sender_mx});
    $self->{state}->{sender_mx} = RFCMail::DNS::MXData->new($from->domain)
	if $from->is_path;
    return ($self->SUPER::mail_from(@_));
}


sub include_xmxinfo_handler {
    my ($self) = @_;
    $self->{handlers}->set('XMXINFO', undef, 'xmxinfo_handler', sub { 'XMXINFO' });
    return ($self);
}


sub xmxinfo_handler {
    my ($self, $cmd) = @_;
    my $mxdata = $self->{state}->{sender_mx};
    return ([211, "No MX info available at this time."])
	if not $mxdata;
    return ([211, "STATUS: ".$mxdata->status,
	     "NAME: ".join(" -> ", @{$mxdata->{cname}}),
	     map { sprintf("MX: %s [%s]", $_->name, join("] [", $_->addrs)) } $mxdata->mxs,
	    ]);
}


1;
